//
//  SelectedDateViewController.m
//  EpointFrame5
//
//  Created by Pill Gong on 14-2-19.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "SelectedDateViewController.h"
#import "ScheduleEditViewContrllerViewController.h"
#import "PGTimeView.h"
#import "UIView+Addtions.h"

@interface SelectedDateViewController ()

@property (nonatomic, strong) UIScrollView *contentView;

@property (nonatomic, strong) UIView *timeView;
@property (nonatomic, strong) UIView *startView;
@property (nonatomic, strong) UIView *endView;
@property (nonatomic, strong) PGTimeView *startTime;
@property (nonatomic, strong) PGTimeView *endTime;

@property (nonatomic, strong) UIView *sendView;
@property (nonatomic, strong) PGTimeView *sendTime;

@property (nonatomic, strong) UIView *pickerContentView;
@property (nonatomic, strong) UIDatePicker *timePicker;

@end

@implementation SelectedDateViewController {
    BOOL msgSwitchOn, endTimeChanged, sendTimeChanged;
    int timeViewTag;
    
    PGTimeView *pStartTime, *pEndTime, *pSendTime;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"时间选择";

    [self setRightBarButtonItemWithTitle:@"保存" action:@selector(save:)];
    
    msgSwitchOn = [[initParam valueForKey:@"msgSwitchOn"] boolValue];
    timeViewTag = [[initParam valueForKey:@"timeViewTag"] intValue];
    
    pStartTime = (PGTimeView *)[initParam valueForKey:@"StartTime"];
    pEndTime = (PGTimeView *)[initParam valueForKey:@"EndTime"];
    pSendTime = (PGTimeView *)[initParam valueForKey:@"SendTime"];

    [self addContentView];
}

- (void)save:(id)sender {
    NSLog(@"save");
    
    if ([_startTime.timeDate earlierDate:_sendTime.timeDate] == _startTime.timeDate) {
        [self toastMessage:@"提醒时间应当小于起始时间！"];
        [self updateStatusAndDate:SendTimeViewTAG];
        return;
    }
    
    if ([_startTime.timeDate earlierDate:_endTime.timeDate] == _endTime.timeDate) {
        [self toastMessage:@"结束时间应当大于起始时间！"];
        [self updateStatusAndDate:EndTimeViewTAG];
        return;
    }
    
    pStartTime.timeDate = _startTime.timeDate;
    pEndTime.timeDate = _endTime.timeDate;
    pSendTime.timeDate = _sendTime.timeDate;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - subview

- (void)addContentView {
    self.contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, AppWidth, AppHeight-SystemTakeHeight)];
    _contentView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    [self.view addSubview:_contentView];
    
    [self addSubviews];
}

- (void)addSubviews {
    [self addTimeView];
    [self addSendView];
    if (msgSwitchOn == NO) {
        _sendView.hidden = YES;
    }
    [self addPickerContentView];
    [self updateStatusAndDate:timeViewTag];
}

- (void)addTimeView {
    self.timeView = [[UIView alloc] initWithFrame:CGRectMake(LeftPadding, TopPadding, CellWidth, CellHeight*2)];
    _timeView.clipsToBounds = YES;
    [self addViewStyle:_timeView];
    [_contentView addSubview:_timeView];
    
    
    self.startView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CellWidth, CellHeight)];
    UITapGestureRecognizer *startTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(startViewTaped:)];
    [_startView addGestureRecognizer:startTap];
    [_timeView addSubview:_startView];
    
    UILabel *startLB = [self getLeftLabel:@"起始"];
    startLB.centerY = CellHeight*0.5;
    [_startView addSubview:startLB];
    
    self.startTime = [[PGTimeView alloc] initWithFrame:CGRectMake(LeftItemWidth, 0, CellWidth-LeftItemWidth, CellHeight) withTap:NO];
    _startTime.timeDate = pStartTime.timeDate;
    _startTime.tag = StartTimeViewTAG;
    [_startView addSubview:_startTime];
    
    
    self.endView = [[UIView alloc] initWithFrame:CGRectMake(0, CellHeight, CellWidth, CellHeight)];
    UITapGestureRecognizer *endTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endViewTaped:)];
    [_endView addGestureRecognizer:endTap];
    [_timeView addSubview:_endView];
    
    UILabel *endLB = [self getLeftLabel:@"结束"];
    endLB.centerY = CellHeight*0.5;
    [_endView addSubview:endLB];
    
    self.endTime = [[PGTimeView alloc] initWithFrame:CGRectMake(LeftItemWidth, 0, CellWidth-LeftItemWidth, CellHeight) withTap:NO];
    _endTime.timeDate = pEndTime.timeDate;
    _endTime.tag = EndTimeViewTAG;
    [_endView addSubview:_endTime];
    
    UIImageView *line = [self getHorizontalLine:CellHeight];
    [_timeView addSubview:line];
}

- (void)addSendView {
    self.sendView = [[UIView alloc] initWithFrame:CGRectMake(LeftPadding, TopPadding, CellWidth, CellHeight)];
    UITapGestureRecognizer *sendTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sendViewTaped:)];
    [_sendView addGestureRecognizer:sendTap];
    _sendView.top = _timeView.bottom+TopPadding;
    [self addViewStyle:_sendView];
    [_contentView addSubview:_sendView];
    
    UILabel *sendLB = [self getLeftLabel:@"提醒"];
    sendLB.centerY = CellHeight*0.5;
    [_sendView addSubview:sendLB];
    
    self.sendTime = [[PGTimeView alloc] initWithFrame:CGRectMake(LeftItemWidth, 0, CellWidth-LeftItemWidth, CellHeight) withTap:NO];
    _sendTime.timeDate = pSendTime.timeDate;
    _sendTime.tag = SendTimeViewTAG;
    [_sendView addSubview:_sendTime];
}

- (void)addPickerContentView {

    CGFloat pickerTop;
    if (msgSwitchOn) {
        pickerTop = _sendView.bottom+TopPadding;
    } else {
        pickerTop = _timeView.bottom+TopPadding;
    }
    
    self.pickerContentView = [[UIView alloc] initWithFrame:CGRectMake(LeftPadding, 0, CellWidth, CellHeight*4)];
    _pickerContentView.top = pickerTop;
    [self addViewStyle:_pickerContentView];
    [_contentView addSubview:_pickerContentView];
    
    self.timePicker = [[UIDatePicker alloc] init];
    _timePicker.frame = CGRectMake(0, 0, CellWidth, CellHeight*4);
    [_timePicker addTarget:self action:@selector(timePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
    _timePicker.datePickerMode =  UIDatePickerModeDateAndTime;
    _timePicker.locale = [NSLocale currentLocale];
    [_pickerContentView addSubview:_timePicker];
}

- (void)startViewTaped:(UITapGestureRecognizer *)startTap {
    if (startTap.state == UIGestureRecognizerStateRecognized) {
        [self updateStatusAndDate:StartTimeViewTAG];
    }
}

- (void)endViewTaped:(UITapGestureRecognizer *)endTap {
    if (endTap.state == UIGestureRecognizerStateRecognized) {
        [self updateStatusAndDate:EndTimeViewTAG];
    }
}

- (void)sendViewTaped:(UITapGestureRecognizer *)sendTap {
    if (sendTap.state == UIGestureRecognizerStateRecognized) {
        [self updateStatusAndDate:SendTimeViewTAG];
    }
}

- (void)updateStatusAndDate:(int)tag {
    
    timeViewTag = tag;
    _startView.backgroundColor = [UIColor whiteColor];
    _endView.backgroundColor = [UIColor whiteColor];
    _sendView.backgroundColor = [UIColor whiteColor];
    
    PGTimeView *tempTimeView;
    
    switch (tag) {
        case StartTimeViewTAG:
            tempTimeView = _startTime;
            _startView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.8];
            break;
        case EndTimeViewTAG:
            tempTimeView = _endTime;
            _endView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.8];
            break;
        case SendTimeViewTAG:
            tempTimeView = _sendTime;
            _sendView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.8];
            break;
        default:
            break;
    }
    
    [_timePicker setDate:tempTimeView.timeDate animated:YES];
}

- (void)addViewStyle:(UIView *)view {
    view.backgroundColor = [UIColor whiteColor];
    view.layer.cornerRadius = 10;
    view.layer.borderWidth = 1;
    view.layer.borderColor = [[UIColor colorWithWhite:0.667 alpha:0.8] CGColor];
}

- (UILabel *)getLeftLabel:(NSString *)text {
    UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, LeftItemWidth, 21)];
    leftLabel.text = text;
    leftLabel.textAlignment = NSTextAlignmentCenter;
    leftLabel.font = [UIFont boldSystemFontOfSize:17.0];
    return leftLabel;
}

- (UIImageView *)getHorizontalLine:(CGFloat)top {
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, top, CellWidth, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    line.alpha = 0.8;
    return line;
}


- (void)timePickerValueChanged:(UIDatePicker *)datePicker {
    NSLog(@"datePicker:%@", datePicker.date);
    switch (timeViewTag) {
        case StartTimeViewTAG:
            _startTime.timeDate = datePicker.date;
            NSTimeInterval startTimeInter = [datePicker.date timeIntervalSince1970];
            if (endTimeChanged == NO) {
                NSTimeInterval endTimeInter = startTimeInter + 60*60;
                _endTime.timeDate = [NSDate dateWithTimeIntervalSince1970:endTimeInter];
            }
            if (sendTimeChanged == NO) {
                NSTimeInterval sendTimeInter = startTimeInter - 30*60;
                _sendTime.timeDate = [NSDate dateWithTimeIntervalSince1970:sendTimeInter];
            }
            break;
        case EndTimeViewTAG:
            endTimeChanged = YES;
            _endTime.timeDate = datePicker.date;
            break;
        case SendTimeViewTAG:
            sendTimeChanged = YES;
            _sendTime.timeDate = datePicker.date;
            break;
        default:
            break;
    }
}

@end
